<?php


function parksresult($category,$lat,$lon)
{	
require "GooglePlaces.php";
include "parks_search.php";//To use the variables and functions present in this
$responseDoc = new DOMDocument();//creates a DOM Document
$responseRoot = $responseDoc->appendChild(
$responseDoc->createElement("Places"));//the root element of the DOM Structure
  	 
$res=parks($category,$lat,$lon);

if(count($res) == 0)
{
  $gplaces = New GooglePlaces;    	 
  $lat_lon_string = $lat.",".$lon;
  $gplaces->SetLocation($lat_lon_string);
  $gplaces->SetRadius(50000);
  $gmaps_category = get_gmaps_type_from_category($category);
  $gplaces->SetTypes($gmaps_category);
  $results = $gplaces->Search();
  //print_r($results);
 // echo status. $results['status'];
 // echo "Lat.".$results['results'][0]['geometry']['location']['lat'];
  for($x=1;$x<count($results['results']);$x++)
  {	
  $Parks_Child=$responseRoot->appendChild($responseDoc->createElement("Place"));
    
  if(isset($results['results'][$x]['name']))
  {  
  $Parks_Child->appendChild($responseDoc->createElement("Name",$results['results'][$x]['name']));//Child of Parks
  }
  else
  {
  $Parks_Child->appendChild($responseDoc->createElement("Name","Not Available"));//Child of Parks  	
  }
  if(isset($results['results'][$x]['icon']))
  {  
  $Parks_Child->appendChild($responseDoc->createElement("Icon",$results['results'][$x]['icon']));//Child of Parks
  }
  else
  {
  $Parks_Child->appendChild($responseDoc->createElement("Icon","Not Available"));//Child of Parks
  }
  //$Parks_Child->appendChild($responseDoc->createElement("Id",$results['results'][$x]['reference']));//child1 of 'Details' ;gives the id of the park
  $gplaces->SetReference($results['results'][$x]['reference']);
  $result_details = $gplaces->Details();
  
  if(isset($result_details['result']['formatted_address']))
  {
  $Parks_Child->appendChild($responseDoc->createElement("Location",$result_details['result']['formatted_address']));//child2 of 'Details'
  }
  else 
  {
  $Parks_Child->appendChild($responseDoc->createElement("Location","Not Available"));//child2 of 'Details'
  }
  
  if(isset($result_details['result']['formatted_phone_number']))
  {
  $Parks_Child->appendChild($responseDoc->createElement("Phone",$result_details['result']['formatted_phone_number']));//child2 of 'Details'
  }
  else 
  {
  $Parks_Child->appendChild($responseDoc->createElement("Phone","Not Available"));//child2 of 'Details'  	
  }
  
  if(isset($result_details['result']['website']))
  {
  $Parks_Child->appendChild($responseDoc->createElement("Website",$result_details['result']['website']));//child2 of 'Details'
  }
  else
  {
  $Parks_Child->appendChild($responseDoc->createElement("Website","Not Available"));
  }
  //$Parks_Child->appendChild($responseDoc->createElement("P_Location","CA"));//child4 of 'Details'
  }
}
else 
{
 #$Parks_Child=$responseRoot->appendChild($responseDoc->createElement("Park"));
 #$Parks_Child->appendChild($responseDoc->createElement("P_Name","name"));//Child of Parks
	        
for($x=0;$x<count($res);$x++)//to loop through each index of $array[] declared in parks_search.php
{         
	        $Parks_Child=$responseRoot->appendChild($responseDoc->createElement("Place"));
	        $Parks_Child->appendChild($responseDoc->createElement("Name",$res[$x][1]));//Child of Parks
			$Parks_Child->appendChild($responseDoc->createElement("Id",$res[$x][0]));//child1 of 'Details' ;gives the id of the park
            $Parks_Child->appendChild($responseDoc->createElement("Location",$res[$x][4].", ".$res[$x][2].", ".$res[$x][3]));//child4 of 'Details'
                      
}
}
return $responseDoc->saveXML();
}

function sendXMLResponse($response)
{

   header("Content-Type: text/xml");
   $response= str_replace('<?xml version="1.0"?>','',$response);
   echo $response;
}

function get_gmaps_type_from_category($category)
{

	if($category == "parks")
	{
		$result = "park";
	}
	else if ($category == "restaurants")
	{
		$result = "food|restaurant";
	}
	else if ($category == "temples")
	{
		$result = "hindu_temple|place_of_worship";
	}
	else if ($category == "theaters")
	{
		$result = "movie_theater";
	}
	
	return $result;
	
}
?>
